﻿#region 命名空间
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;
#endregion

namespace ContentFactory
{
    /// <summary>
    /// 项目设置对话框。
    /// </summary>
    public partial class ProjectSettingsDialog : Window
    {
        #region 属性

        /// <summary>
        /// 当前项目。
        /// </summary>
        public Project Project
        {
            get { return DataContext as Project; }
            set { DataContext = value; }
        }

        #endregion

        #region 初始化

        /// <summary>
        /// 初始化。
        /// </summary>
        public ProjectSettingsDialog()
        {
            InitializeComponent();
        }

        #endregion

        #region 事件处理

        // 选择目录 按钮点击后。
        private void Button_BrowserDirectory_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            Configuration configuration = button.DataContext as Configuration;
            CommonOpenFileDialog dialog = new CommonOpenFileDialog()
            {
                Title = string.Format("选择针对 {0} 平台的游戏目录：", configuration.GamePlatform),
                IsFolderPicker = true,
            };
            if (dialog.ShowDialog(this) == CommonFileDialogResult.Ok)
            {
                configuration.ProjectDirectory = dialog.FileName;
            }
        }

        // 选择项目 按钮点击后。
        private void Button_BrowserFile_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            Configuration configuration = button.DataContext as Configuration;
            CommonOpenFileDialog dialog = new CommonOpenFileDialog()
            {
                Title = string.Format("选择针对 {0} 平台的游戏项目：", configuration.GamePlatform),
                DefaultExtension = ".csproj",
            };
            dialog.Filters.Add(new CommonFileDialogFilter("游戏项目(*.csproj)", "*.csproj"));
            if (dialog.ShowDialog(this) == CommonFileDialogResult.Ok)
            {
                configuration.ProjectFile = dialog.FileName;
            }
        }

        // 清除项目 按钮点击后。
        private void Button_Clear_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            Configuration configuration = button.DataContext as Configuration;
            configuration.ProjectFile = null;
        }

        // 窗体关闭时。
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            bool hasBuild = false;
            foreach (var configuration in Project.Configurations)
            {
                if (configuration.Build)
                {
                    hasBuild = true;
                    if (!Directory.Exists(configuration.ProjectDirectory))
                    {
                        MessageBox.Show(this, string.Format("已开启针对 {0} 平台素材生成功能，但项目目录未设置或设置错误！", configuration.GamePlatform), "Content Factory", MessageBoxButton.OK, MessageBoxImage.Warning);
                        e.Cancel = true;
                        break;
                    }
                }
            }
            if (!hasBuild)
            {
                MessageBoxResult result = MessageBox.Show(this, "未针对任何平台开启素材生成功能，是否保持当前设置？", "Content Factory", MessageBoxButton.YesNo, MessageBoxImage.Question);
                e.Cancel = result != MessageBoxResult.Yes;
            }
        }

        #endregion
    }
}
